<?php
namespace Home\Controller;

use Think\Controller;

class BaseController extends Controller
{
    /**
    * 登录验证
    */
    public function __construct(){
        parent::__construct();

        $nowUrl = $_SERVER['PHP_SELF'];
        if(strpos($nowUrl, "submitReqLeft") !== false || strpos($nowUrl, "submitReqRightOne") !== false || strpos($nowUrl, "submitReqRightTwo") !== false){
            if(!isset($_SESSION["Home"]["email"])) {
                $_SESSION["Home"]["uid"] = 1;
                header("location:".U('Home/User/serviceCenter'));
                return false;
            }
        } 
        if(strpos($nowUrl, "pcMyProjects") !== false || strpos($nowUrl, "personalCenter") !== false){
            if(!isset($_SESSION["Home"]["email"])) {
                $_SESSION["Home"]["uid"] = 1;
                header("location:".U('Home/User/main'));
                return false;
            }
        }
        if(empty($_SESSION["Home"]["uid"])){
            header("location:".U('Home/User/main'));
            $_SESSION["Home"]["uid"] = 1;
            return false;
        }
    }

    /**
     * 个人中心--我的项目--分页
     * $where 条件
     * $field 字段
     * $order 排序
     * $page_count 每页显示记录数
     */
    public function showMyProjectsPages($where = null, $field = '*', $order = 'r_submitTime DESC', $page_count = 3)
    {
        $mod = M("Requirement");
        
        $subQuery = $mod->where($where)->order($order)->field($field)->buildSql();// 子查询
        $count = $mod->table($subQuery.' a')->where("a.r_projectname != ''")->count(); // 总记录数
        
        $Page = new \Think\Page($count, $page_count); // 传入总记录数和每页显示的记录数                                             
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $mod->table($subQuery.' a')->where("a.r_projectname != ''")->limit($Page->firstRow . ',' . $Page->listRows)->select();
            // ->limit(($pageNum-1)*3 . ',' . $Page->listRows)

        $Page->lastSuffix = false;
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '末页');
        $Page->setConfig('first', '首页');
        $Page->setConfig('theme', '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% %HEADER%');
        $Page->parameter = I('get.');
        $show = $Page->show(); // 分页显示输出
        $this->assign('page', $show); // 赋值分页输出
        return $list;
    }
}